package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.SysRebate;
import cn.stylefeng.guns.modular.yingbao.entity.SysSellLog;
import cn.stylefeng.guns.modular.yingbao.mapper.SysSellLogMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.SysSellLogParam;
import cn.stylefeng.guns.modular.yingbao.model.result.SysSellLogResult;
import  cn.stylefeng.guns.modular.yingbao.service.SysSellLogService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 挂售日志 服务实现类
 * </p>
 *
 * @author jj
 * @since 2019-12-11
 */
@Service
public class SysSellLogServiceImpl extends ServiceImpl<SysSellLogMapper, SysSellLog> implements SysSellLogService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysSellLogParam param){
        SysSellLog entity = getEntity(param);
        this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysSellLogParam param){
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysSellLogParam param){
        SysSellLog oldEntity = getOldEntity(param);
        SysSellLog newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public SysSellLogResult findBySpec(SysSellLogParam param){
        return null;
    }

    @Override
    public List<SysSellLogResult> findListBySpec(SysSellLogParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(SysSellLogParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void deleteSysSell() {
        Date today = new Date();
        //获取三十天前日期
        Calendar theCa = Calendar.getInstance();
        theCa.setTime(today);
        theCa.add(theCa.DATE, -180);//最后一个数字30可改，30天的意思
        Date start = theCa.getTime();
        int create_date = baseMapper.delete(new QueryWrapper<SysSellLog>().lt("create_time", start));
        if (create_date>0){
            System.out.println("成功");
        }
    }

    private Serializable getKey(SysSellLogParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private SysSellLog getOldEntity(SysSellLogParam param) {
        return this.getById(getKey(param));
    }

    private SysSellLog getEntity(SysSellLogParam param) {
        SysSellLog entity = new SysSellLog();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
